package com.che.software.testato.business;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.che.software.testato.business.exception.ServiceCreationManagerException;
import com.che.software.testato.business.exception.ServiceSearchManagerException;
import com.che.software.testato.domain.dao.IServiceDAO;
import com.che.software.testato.domain.dao.jdbc.exception.ServiceCreationDAOException;
import com.che.software.testato.domain.dao.jdbc.exception.ServiceSearchDAOException;
import com.che.software.testato.domain.entity.StatisticalService;
import com.che.software.testato.domain.entity.search.ServiceSearch;

/**
 * Manager dedicated to the services.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @since July, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
@Service("serviceManager")
public class ServiceManager {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(ServiceManager.class);

	/**
	 * Members.
	 */
	@Autowired
	private IServiceDAO serviceDAO;

	/**
	 * Creates a service from his label.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param label the label of the service to create.
	 * @since July, 2011.
	 * @throws ServiceCreationManagerException if an error occurs during the
	 *         creation.
	 */
	public void createServiceFromLabel(String label) throws ServiceCreationManagerException {
		LOGGER.debug("createServiceFromLabel(" + label + ").");
		try {
			serviceDAO.createServiceFromLabel(label);
		} catch (ServiceCreationDAOException e) {
			throw new ServiceCreationManagerException(e);
		}
	}

	/**
	 * Service search from an user id.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param userId the user id.
	 * @return the resulting object list.
	 * @since July, 2011.
	 * @throws ServiceSearchManagerException if an error occurs during the
	 *         search.
	 */
	public com.che.software.testato.domain.entity.Service searchServiceFromUserId(int userId) throws ServiceSearchManagerException {
		LOGGER.debug("searchServiceFromUserId(" + userId + ").");
		try {
			return serviceDAO.searchServiceFromUserId(userId);
		} catch (ServiceSearchDAOException e) {
			throw new ServiceSearchManagerException(e);
		}
	}

	/**
	 * Service search from a bean of criterions.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param searchBean the criterions to use for the search.
	 * @return the resulting object list.
	 * @since July, 2011.
	 * @throws ServiceSearchManagerException if an error occurs during the
	 *         search.
	 */
	public List<com.che.software.testato.domain.entity.Service> searchService(ServiceSearch searchBean) throws ServiceSearchManagerException {
		LOGGER.debug("searchService().");
		try {
			return serviceDAO.searchService(searchBean);
		} catch (ServiceSearchDAOException e) {
			throw new ServiceSearchManagerException(e);
		}
	}

	/**
	 * Service (with statistics) search from a bean of criterions.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param searchBean the criterions to use for the search.
	 * @return the resulting object list.
	 * @since July, 2011.
	 * @throws ServiceSearchManagerException if an error occurs during the
	 *         service search.
	 */
	public List<StatisticalService> searchStatisticalService(ServiceSearch searchBean) throws ServiceSearchManagerException {
		LOGGER.debug("searchStatisticalService().");
		List<StatisticalService> result = new ArrayList<StatisticalService>();
		for (com.che.software.testato.domain.entity.Service service : searchService(searchBean)) {
			try {
				result.add(new StatisticalService(serviceDAO.getUsersNumberFromServiceId(service.getServiceId()), service, serviceDAO.getLastHiredUserIdFromServiceId(service.getServiceId())));
			} catch (ServiceSearchDAOException e) {
				throw new ServiceSearchManagerException(e);
			}
		}
		return result;
	}

	/**
	 * Getter for the private field value serviceDAO.
	 * 
	 * @return the serviceDAO field value.
	 */
	public IServiceDAO getServiceDAO() {
		return serviceDAO;
	}

	/**
	 * Setting a value to the serviceDAO field.
	 * 
	 * @param serviceDAO the value to set.
	 */
	public void setServiceDAO(IServiceDAO serviceDAO) {
		this.serviceDAO = serviceDAO;
	}
}